var userAgent = navigator.userAgent;
var isAndroid = userAgent.indexOf('Android') > -1 || userAgent.indexOf('Adr') > -1; //android终端
var isiOS = !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
var isWeChat = userAgent.toLowerCase();

function isApp(client) {
    if (client == 'ios' || client == 'android') {
        return true;
    } else {
        return false;
    }
}

/**
 * app 登录
 * */
function toLogin(url, client) {
    if (isWeChat.match(/MicroMessenger/i) == 'micromessenger' || !isApp(client)) {
        window.location.href = url;
    } else {
        if (isAndroid == true) {
            window.android.toLogin();
        }
        if (isiOS == true) {
            window.webkit.messageHandlers.toLogin.postMessage(null);
        }
    }
}


/**
 * app 注册
 * */
function toRegister(url, client) {
    if (isWeChat.match(/MicroMessenger/i) == 'micromessenger' || isApp(client) == false) {
        window.location.href = url;
    } else {
        if (isAndroid == true && client == 'android') {
            window.android.goRegister();
        }
        if (isiOS == true && client == 'ios') {
            window.webkit.messageHandlers.goRegister.postMessage(null);
        }
    }
}

/**
 * app 分享
 * */
function toShare(client) {
    if (!isApp(client)) {
        return false;
    } else {
        if (isAndroid == true) {
            window.android.toShare();
        }
        if (isiOS == true) {
            window.webkit.messageHandlers.toShare.postMessage(null);
        }
    }
}


/**
 * app 我要投资
 * */
function toFinance(url, client) {
    if (!isApp(client)) {
        window.location.href = url;
        return false;
    } else {
        if (isAndroid == true) {
            window.android.toFinance();
        }
        if (isiOS == true) {
            window.webkit.messageHandlers.toFinance.postMessage(null);
        }
    }
}

/**
 * app 一般分享
 * */
function generalShare(title, des, link, image, client) {
    if (!isApp(client)) {
        return false;
    } else {
        if (isAndroid == true) {
            window.android.generalShare({title: title, desc: des, url: link, image: image});
        }
        if (isiOS == true) {
            window.webkit.messageHandlers.generalShare.postMessage({title: title, desc: des, url: link, image: image});
        }
    }
}


/**
 * app 自动投标
 * */
function setAutoborrow(url, client) {
    if (isWeChat.match(/MicroMessenger/i) == 'micromessenger' || !isApp(client)) {
        window.location.href = url;
    } else {
        if (isAndroid == true) {
            window.android.setAutoborrow();
        }
        if (isiOS == true) {
            window.webkit.messageHandlers.setAutoborrow.postMessage(null);
        }
    }
}

/**
 * app 登录后刷新  app调用此方法
 * */
function login_refresh(token) {
    if (token != '' && token != gettoken) {
        var new_url, url;
        if (gettoken) {
            url = changeUrlArg(location.href, 'token', token);
        }
        else {
            url = location.href + '&token=' + token;
        }
        if (url.indexOf("#luck") > 0) {
            var id = url.substr(url.indexOf("#"), 5);
            new_url = url.split(id).join("") + id;
            location.href = new_url;
        } else {
            location.href = url;
        }
    }
}

function changeUrlArg(url, arg, val) {
    var pattern = arg + '=([^&]*)';
    var replaceText = arg + '=' + val;
    return url.match(pattern) ? url.replace(eval('/(' + arg + '=)([^&]*)/gi'), replaceText) : (url.match('[\?]') ? url + '&' + replaceText : url + '?' + replaceText);
}


/**
 * app 卡券包
 * */
function toCard(client) {
    if (!isApp(client)) {
        return false;
    } else {
        if (isAndroid == true) {
            window.android.toCard();
        }
        if (isiOS == true) {
            window.webkit.messageHandlers.toCard.postMessage(null);
        }
    }
}